package com.jy.adscm.operation.cgxsb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cgxsb.bean.SporadicPurchase;
import com.jy.adscm.operation.cgxsb.bean.SporadicPurchasedetail;
import com.jy.adscm.operation.cgxsb.bean.SporadicPurchaseinfo;
import com.jy.adscm.operation.cgxsb.mapper.mysql.SporadicPurchaseMapper;
import com.jy.adscm.operation.cgxsb.mapper.mysql.SporadicPurchasedetailMapper;
import com.jy.adscm.operation.cgxsb.mapper.mysql.SporadicPurchaseinfoMapper;
import com.jy.adscm.operation.cgxsb.service.SporadicPurchaseService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-03-12
 */
@Service
public class SporadicPurchaseServiceImpl extends ServiceImpl<SporadicPurchaseMapper, SporadicPurchase> implements SporadicPurchaseService {
    @Autowired
    SporadicPurchaseinfoMapper sporadicPurchaseinfoMapper;
    @Autowired
    SporadicPurchasedetailMapper sporadicPurchasedetailMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyDeptMapper jyDeptMapper;
    @Override
    public boolean insertSporadicPurchase(SporadicPurchase sporadicPurchase) throws AdscmSystemException {
        boolean insert = super.insert(sporadicPurchase);
        if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getTaskContent(),null)){
            SporadicPurchaseinfo sporadicPurchaseinfo =new  SporadicPurchaseinfo();
            sporadicPurchaseinfo.setType("1");
            sporadicPurchaseinfo.setContent(sporadicPurchase.getTaskContent());
            sporadicPurchaseinfo.setPerson(sporadicPurchase.getCreator());
            sporadicPurchaseinfo.setTime(new Date());
            sporadicPurchaseinfo.setTid(sporadicPurchase.getId().toString());
            sporadicPurchaseinfoMapper.insert(sporadicPurchaseinfo);
        }
        return insert;
    }

    @Override
    public boolean deleteSporadicPurchase(Integer id, Token token) throws AdscmSystemException {
        SporadicPurchase sporadicPurchase = super.selectById(id);
        if ("1".equals(sporadicPurchase.getIsEveryDayTask())){
            throw new AdscmSystemException("零星采购任务不能删除");
        }
        if (sporadicPurchase.getTaskState()==1){
            throw new AdscmSystemException("零星采购已经结束");
        }
        if (!sporadicPurchase.getCreator().equals(token.getUser().getId().toString())){
            throw new AdscmSystemException("您不能删除该任务");
        }
        //找到下面的明细删除
        Wrapper< SporadicPurchasedetail> wrapper =new EntityWrapper<>();
        wrapper.eq("task_id",id);
        List<SporadicPurchasedetail> sporadicPurchasedetails = sporadicPurchasedetailMapper.selectList(wrapper);
        for (SporadicPurchasedetail sporadicPurchasedetail : sporadicPurchasedetails) {
            sporadicPurchasedetailMapper.deleteById(sporadicPurchasedetail.getId());
        }
        boolean b = super.deleteById(id);
        return b;
    }

    @Override
    public Boolean updateSporadicPurchase(SporadicPurchase sporadicPurchase, Token token) throws AdscmSystemException {
        if (sporadicPurchase.getCreator().equals(String.valueOf(token.getUser().getId()))){

            sporadicPurchase.setIsCreatorUpdate(1);
        }
        if (sporadicPurchase.getPerformer().equals(String.valueOf(token.getUser().getId()))){
            sporadicPurchase.setIsPerformerUpdate(1);
        }
        SporadicPurchase oldsporadicPurchase = super.selectById(sporadicPurchase.getId());
        //每日任务且是自己登陆的
        if (sporadicPurchase.getCreator().equals(sporadicPurchase.getPerformer())&&sporadicPurchase.getCreator().equals(token.getUser().getId().toString())){
            if(AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getEndDate(),null)){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(oldsporadicPurchase.getEndDate(),null)){
                    sporadicPurchase.setTaskState(1);
                }
            }
        } else {
            //如果是未接收的，且登录人不是创建人就不能做修改了
            if ("-1".equals(sporadicPurchase.getIsReceived())&&!sporadicPurchase.getCreator().equals(token.getUser().getId().toString())){
                throw  new AdscmSystemException("不是创建人不能修改");
            }
            //判断是接收
            if (!"-1".equals(sporadicPurchase.getIsReceived())){
                sporadicPurchase.setReceivedDate(new Date());
            }
            //判断是否是开始任务
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getStartDate(),null)){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getReceivedDate(),null)){
                    throw  new AdscmSystemException("任务未接收不能填写开始时间");
                }
                if (!AdscmBaseUtils.getInstance().isNotEmpty(oldsporadicPurchase.getStartDate(),null)){
                    sporadicPurchase.setStartCreateDate(new Date());
                }
            }

            //判断任务是否已经结束
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getEndDate(),null)){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getStartDate(),null)){
                    throw  new AdscmSystemException("没有开始时间不能填写结束始时间");
                }

                if (!AdscmBaseUtils.getInstance().isNotEmpty(oldsporadicPurchase.getEndDate(),null)){
                    sporadicPurchase.setEndDateCreateDate(new Date());
                }

            }
            //如果复核记录不等于空
            if (!"-1".equals(sporadicPurchase.getIsReviewTask())){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getEndDate(),null)){
                    throw  new AdscmSystemException("任务未结束，不能进行复核");
                }
                sporadicPurchase.setReviewTime(new Date());
                if ("1".equals(sporadicPurchase.getReviewResult())){
                    sporadicPurchase.setTaskState(1);
                }
            }
        }
        boolean result = super.updateById(sporadicPurchase);
        return result;
    }

    @Override
    public List<SporadicPurchase> selectSporadicPurchase(Token token) throws AdscmSystemException {
        List<SporadicPurchase> sporadicPurchases=new ArrayList<>();
        //首先查询是否是管理员
        if (AdscmBaseUtils.getInstance().isNotEmpty(token.getUser().getId(),null)){
            Wrapper<SporadicPurchase> wrapper =new EntityWrapper<>();
            wrapper.eq("creator",token.getUser().getId());
            wrapper.eq("task_state",0);
            wrapper.ne("performer",token.getUser().getId());
            wrapper.orderBy("task_state",true);
            wrapper.orderBy("create_date",false);
            sporadicPurchases = super.selectList(wrapper);

        }else {
            throw new AdscmSystemException("未获取到登录消息");
        }
        for (SporadicPurchase sporadicPurchase : sporadicPurchases) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(sporadicPurchase.getCreator());
                sporadicPurchase.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getPerformer(),null)){
                JyUser jyUser = jyUserMapper.selectById(sporadicPurchase.getPerformer());
                sporadicPurchase.setPerformerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(sporadicPurchase.getDept());
                sporadicPurchase.setDeptName(jyDept.getDeptName());
            }
        }
        return sporadicPurchases;
    }

    @Override
    public List<SporadicPurchase> selectMySporadicPurchase(Integer taskState, Token token) {
        Date formateDate = AdscmDateUtils.getFormateDate(new Date());
        Wrapper<SporadicPurchase> wrapper =new EntityWrapper<>();
        wrapper.eq("task_state",taskState);
        wrapper.eq("performer",token.getUser().getId());
        wrapper.eq("isEveryDayTask","0");
        List<SporadicPurchase> sporadicPurchases = super.selectList(wrapper);
        if (taskState==0){
            Wrapper<SporadicPurchase> wrapper1 =new EntityWrapper<>();

            wrapper1.eq("performer",token.getUser().getId());
            wrapper1.eq("isEveryDayTask","1");
            wrapper1.eq("finish_date",formateDate);
            wrapper1.orderBy("create_date",false);
            List<SporadicPurchase> sporadicPurchases1 = super.selectList(wrapper1);
            for (SporadicPurchase sporadicPurchase : sporadicPurchases1) {
                sporadicPurchases.add(sporadicPurchase);
            }
        }
        if (taskState==1){
            Wrapper<SporadicPurchase> wrapper2 =new EntityWrapper<>();
            wrapper2.eq("performer",token.getUser().getId());
            wrapper2.eq("isEveryDayTask","1");
            wrapper2.lt("finish_date",formateDate);
            wrapper2.orderBy("create_date",false);
            List<SporadicPurchase> sporadicPurchases2 = super.selectList(wrapper2);
            for (SporadicPurchase sporadicPurchase : sporadicPurchases2) {
                sporadicPurchases.add(sporadicPurchase);
            }
        }

        for (SporadicPurchase sporadicPurchase : sporadicPurchases) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(sporadicPurchase.getCreator());
                sporadicPurchase.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getPerformer(),null)){
                JyUser jyUser = jyUserMapper.selectById(sporadicPurchase.getPerformer());
                sporadicPurchase.setPerformerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(sporadicPurchase.getDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(sporadicPurchase.getDept());
                sporadicPurchase.setDeptName(jyDept.getDeptName());
            }
        }
        return sporadicPurchases;
    }
}
